package com.stylefeng.guns.modular.pay.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.pay.model.PayWay;
import com.stylefeng.guns.modular.pay.service.IPayWayService;

/**
 * 支付方式控制器
 *
 * @author fengshuonan
 * @Date 2018-08-24 15:21:18
 */
@Controller
@RequestMapping("/payWay")
public class PayWayController extends BaseController {

	private String PREFIX = "/pay/payWay/";

	@Autowired
	private IPayWayService payWayService;

	/**
	 * 跳转到支付方式首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "payWay.html";
	}

	/**
	 * 跳转到添加支付方式
	 */
	@RequestMapping("/payWay_add")
	public String payWayAdd() {
		return PREFIX + "payWay_add.html";
	}

	/**
	 * 跳转到修改支付方式
	 */
	@RequestMapping("/payWay_update/{payWayId}")
	public String payWayUpdate(@PathVariable Integer payWayId, Model model) {
		PayWay payWay = payWayService.selectById(payWayId);
		model.addAttribute("item", payWay);
		LogObjectHolder.me().set(payWay);
		return PREFIX + "payWay_edit.html";
	}

	/**
	 * 获取支付方式列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		return payWayService.selectList(null);
	}

	/**
	 * 新增支付方式
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(PayWay payWay) {
		int count = payWayService.selectCount(new EntityWrapper<PayWay>().eq("pay_way_code", payWay.getPayWayCode()));
		if (count > 0) {
			throw new GunsException(BizExceptionEnum.WAY_EXIST);
		}
		payWay.setCreateTime(new Date());
		payWay.setStatus(1);
		payWayService.insert(payWay);
		return SUCCESS_TIP;
	}

	/**
	 * 删除支付方式
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Integer payWayId) {
		payWayService.deleteById(payWayId);
		return SUCCESS_TIP;
	}

	/**
	 * 修改支付方式
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(PayWay payWay) {
		payWay.setUpdateTime(new Date());
		payWayService.updateById(payWay);

		return SUCCESS_TIP;
	}

	/**
	 * 支付方式详情
	 */
	@RequestMapping(value = "/detail/{payWayId}")
	@ResponseBody
	public Object detail(@PathVariable("payWayId") Integer payWayId) {
		return payWayService.selectById(payWayId);
	}
}
